package com.jiujichina.exception;/**
 * Created by liyi on 2018/5/31.
 */

import com.jiujichina.base.ResultData;
import org.apache.http.impl.bootstrap.HttpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : liyi
 * @program : springboot-provider-user
 * @description : 全局异常捕获
 * @create: 2018-05-31 10-43
 **/
@CrossOrigin
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler
    public ResultData processException(Exception ex, HttpServletRequest request, HttpServletResponse response){
        if (ex instanceof BizException) {
            BizException bizException = (BizException) ex;
            LOGGER.error("业务异常{}"+bizException.getMessage());
            return new ResultData("业务异常", bizException);
        }
        LOGGER.error(ex.toString());
        return new ResultData();
    }
}
